package tech.yunland.shop.domain.dao;

import com.moyuer.bsf.domain.dao.BaseDao;
import com.moyuer.bsf.domain.model.Organ;
import com.moyuer.bsf.domain.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import tech.yunland.shop.domain.model.StoreBill;

import java.util.Date;

/**
 * 出入库单生成器
 *
 * @author 王睿智<geekrize@gmail.com>
 */
@Repository
public class StoreGenerator extends BaseDao<StoreBill, Long> {

	@Autowired
	private SNGenerator snGenerator;

	/**
	 * 销售单生成销售出库单
	 *
	 * @param key 单据类型（参数）
	 * @param bizdate 业务日期
	 * @param creator 创建人
	 * @param organ 机构
	 * @param bizBillId 来源单据ID
	 * @return String
	 */
	public String gen(String key, Date bizdate, User creator, Organ organ, String bizBillId) {
		// DetachedCriteria criteria =
		// DetachedCriteria.forClass(StoreBill.class);
		// if(key=="sale"){
		// criteria.add(Restrictions.eq("bizdate", bizdate));
		// criteria.add(Restrictions.eq("organId",organId));
		// criteria.add(Restrictions.eq("kindcode",1));
		//
		// }
		StoreBill storeBill = new StoreBill();
		storeBill.setBizdate(bizdate);
		storeBill.setOrgan(organ);
		storeBill.setBizBillId(bizBillId);
		//创建单据编号
		if (storeBill.getSn() == null) {
			storeBill.setSn(snGenerator.gen("saleStore", storeBill.getOrganId(), bizdate));
		}
		// 判断创建人
		if (storeBill.getCreator() == null) {
			storeBill.setCreator(creator);
		}
		//判断单据种类
		if (key == "saleStore") {
			storeBill.setDirection(-1);
			storeBill.setBiz_type(31);
		}

		return null;

	}

}
